package leetcode;

/**
 * @author Liangyaoyun
 */
public class leetCode141 {
    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public boolean hasCycle(ListNode head) {
        ListNode frist=head;
        ListNode last=head;
        if (frist==null){
            return false;
        }
        while (frist.next!=null&&last!=null){
            frist=frist.next;
            if(last.next==null){
                return false;
            }
            last=last.next.next;
            if (frist==last){
                return true;
            }
        }
        return false;
    }
}
